﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmTHPhieuXuatDC : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private bool allowCloseForm = true;
        string KhachHang = "";
        string TuNgay = "";
        string TrangThaiThanhToan = "";
        string TinhTrangXuatKho = "";
        private MatHangBOCollection matHangBOCollection = null;
        private PhieuXuatDCBOCollection PhieuXuatDCBOCollection = PhieuXuatDCBOCollection.NewPhieuXuatDCBOCollection();
        private PhieuXuatDCChiTietBOCollection PhieuXuatDCChiTietBOCollection = PhieuXuatDCChiTietBOCollection.NewPhieuXuatDCChiTietBOCollection();
        private PhieuXuatDCBO PhieuXuatDCBO = PhieuXuatDCBO.NewRootPhieuXuatDCBO();

        #endregion variant

        //=====================================
        #region form
        public FrmTHPhieuXuatDC()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmTHPhieuXuatDC_Load);
            this.FormClosing += new FormClosingEventHandler(FrmTHPhieuXuatDC_FormClosing);
            this.btnHuy.Click += new EventHandler(btnHuy_Click);
            this.btnTimKiem.Click += new EventHandler(btnTimKiem_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
            this.grdViewMatHang.RowCellStyle += new RowCellStyleEventHandler(grdView_RowCellStyle);
            this.grdViewMatHang.ShowingEditor += new CancelEventHandler(grdView_ShowingEditor);
        }

        private void FrmTHPhieuXuatDC_Load(object sender, EventArgs e)
        {
            RefreshData();
            //SetFormStatus(this.formStatus);

            Utility.InitControl(this.reLookUpMatHangID);
            Utility.InitControl(this.reLookUpDonViTinhID);

            Utility.InitControl(this.lookUpEditKhachHang);

            Utility.InitControl(this.reLookUpKhachHangID);


        }

        private void FrmTHPhieuXuatDC_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }

        #endregion form

        //=====================================
        #region control

        #endregion control

        //=====================================
        #region button
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion button

        //=====================================
        #region grid

        private void grdView_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            FilterMatHang();
        }

        private void grdView_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView grdView = (GridView)sender;

            bool del = StaticUtil.GetBool(grdView.GetFocusedRowCellValue("DELE"));
            string fieldName = grdView.FocusedColumn.FieldName;

            if (del && fieldName != "DELE")
                e.Cancel = true;
            else
                if (fieldName == "DELE" && grdView.FocusedRowHandle == GridControl.NewItemRowHandle)
                    e.Cancel = true;
        }

        private void grdView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            bool del = StaticUtil.GetBool(this.grdViewMatHang.GetRowCellValue(e.RowHandle, "DELE"));

            if (del)
            {
                e.Appearance.Font = MyStyle.DELETE_FONT;
                e.Appearance.ForeColor = MyStyle.DELETE_COLOR;
            }
        }

        #endregion grid

        //=====================================
        #region function
        private void FilterMatHang()
        {
            //Session.NhomHangID = StaticUtil.GetRealInt(this.grdView.GetFocusedRowCellValue("NhomHangID"));

            //if (Session.NhomHangID == -1)
            //{
            //    //this.grdViewPhieuXuatDCChiTiet.ActiveFilterString = "MatHangID=" + int.MaxValue;

            //    this.giaBOCollection = GiaBOCollection.GetGiaBOCollection();
            //    this.bdSourceGia.DataSource = this.giaBOCollection.Clone();
            //}
            //else
            //{
            //    MatHangBOCollection matHangBOCollection = MatHangBOCollection.GetMatHangBOCollection("NhomHangID = " + Session.NhomHangID.ToString());
            //    string strWhere = "";
            //    string listID = "";
            //    int recordCount = matHangBOCollection.Count;
            //    if (recordCount > 0)
            //    {
            //        for (int i = 0; i < recordCount; i++)
            //        {
            //            object _idVattu = matHangBOCollection[i].MatHangID;
            //            listID += _idVattu.ToString() + ",";
            //        }
            //    }
            //    strWhere = strWhere + " MatHangID in ('" + listID.Replace(",", "','") + "')";
            //    this.giaBOCollection = GiaBOCollection.GetGiaBOCollection(strWhere);
            //    this.bdSourceGia.DataSource = this.giaBOCollection.Clone();
            //}
            //this.grdViewPhieuXuatDCChiTiet.CheckLoaded();
            //SetSttPhieuXuatDCChiTiet();
        }
        private void ResetData()
        {
            lookUpEditKhachHang.EditValue = null;
            this.PhieuXuatDCBO = Model.PhieuXuatDCBO.NewRootPhieuXuatDCBO();
            this.PhieuXuatDCChiTietBOCollection = Model.PhieuXuatDCChiTietBOCollection.NewPhieuXuatDCChiTietBOCollection();
        }

        private void RefreshData()
        {
            ResetData();
            #region

            DanhMucBOList donViTinhBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DonViTinh);
            Utility.SetData(this.reLookUpDonViTinhID, donViTinhBOList, donViTinhBOList.Count);

            MatHangBOList matHangBOList = MatHangBOList.GetMatHangBOList();
            Utility.SetData(this.reLookUpMatHangID, matHangBOList, matHangBOList.Count);

            KhachHangBOList khachHangBOList = KhachHangBOList.GetKhachHangBOList();
            Utility.SetData(this.reLookUpKhachHangID, khachHangBOList, khachHangBOList.Count);
            Utility.SetData(this.lookUpEditKhachHang, khachHangBOList, khachHangBOList.Count);

            dateEditTungay.DateTime = dateEditDenngay.DateTime = DateTime.Now;
            #endregion BOList
            this.PhieuXuatDCBOCollection = Model.PhieuXuatDCBOCollection.NewPhieuXuatDCBOCollection();

            this.bdSourcePhieuXuatDC.DataSource = this.PhieuXuatDCBOCollection.Clone();
            this.matHangBOCollection = MatHangBOCollection.GetMatHangBOCollection();
            this.bdSourceMatHang.DataSource = this.matHangBOCollection.Clone();
            FilterMatHang();
        }

        #endregion function


        private void btnTimKiem_Click(object sender, EventArgs e)
        {

            string strWhere = "PhieuXuatDCID >= 0";
            if (checkThanhToan.Checked)
            {
                strWhere = strWhere + "and ThanhToan = 1";
                TrangThaiThanhToan = "Trạng thái Hóa đơn: Đã Thanh toán (thanh toán đủ,công nợ)";
            }
            else
            {
                strWhere = strWhere + "and ThanhToan = 0";
                TrangThaiThanhToan = "Trạng thái Hóa đơn: Chưa Thanh toán";
            }

            if (checkNgay.Checked)
            {
                strWhere = strWhere + " and (convert(nvarchar,NgayLapPhieu,111) >= '" + dateEditTungay.DateTime.ToString("yyyy/MM/dd") + "') and (convert(nvarchar,NgayLapPhieu,111) <= '" + dateEditDenngay.DateTime.ToString("yyyy/MM/dd") + "')";
                TuNgay = "Ngày lập Hóa đơn: Từ ngày " + dateEditTungay.DateTime.ToString("dd/MM/yyyy") + " Đến ngày " + dateEditDenngay.DateTime.ToString("dd/MM/yyyy");
            }
            else
                TuNgay = "Ngày lập Hóa đơn: Tất cả các ngày ";


            if (checkNhapKho.Checked)
            {
                strWhere = strWhere + "and TinhTrangXuatKho = 1 ";
                TuNgay = "Tình trạng xuất kho: Đã xuất ra kho";
            }
            else
            {
                strWhere = strWhere + "and TinhTrangXuatKho = 0 ";
                TuNgay = "Tình trạng xuất kho: Phiếu xuất đang xử lý, Chưa xuất ra kho";
            }


            if (checkKhachHang.Checked)
                if (lookUpEditKhachHang.EditValue != null)
                {
                    strWhere = strWhere + " and KhachHangID = " + lookUpEditKhachHang.EditValue.ToString();
                    KhachHang = "Khách hàng: " + lookUpEditKhachHang.Text.ToString();
                }
                else
                    KhachHang = "Khách hàng: Tất cả Khách hàng";
            else
                KhachHang = "Khách hàng: Tất cả Khách hàng";

            timKiem(strWhere);
        }


        private void timKiem(string strwhere)
        {

            PhieuXuatDCBOCollection = Model.PhieuXuatDCBOCollection.GetPhieuXuatDCBOCollection(strwhere, "PhieuXuatDCID", "DESC");
            this.bdSourcePhieuXuatDC.DataSource = this.PhieuXuatDCBOCollection.Clone();

            //tinhtien();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            RPhieuXuatDCBOCollection rPhieuXuatDCBOColecction = RPhieuXuatDCBOCollection.NewRPhieuXuatDCBOCollection();
            int rowcount = PhieuXuatDCBOCollection.Count;
            if (rowcount > 0)
                for (int i = 0; i < rowcount; i++)
                {
                    RPhieuXuatDCBO rPhieuXuatDCBO = RPhieuXuatDCBO.NewRootRPhieuXuatDCBO();
                    rPhieuXuatDCBO.CopyFromRPhieuXuatDCBO(PhieuXuatDCBOCollection[i]);
                    rPhieuXuatDCBO.STT0 = i + 1;
                    rPhieuXuatDCBOColecction.Add(rPhieuXuatDCBO);
                }

            Report.rpTongHopPhieuXuatDC rTongHopPhieuXuatDC = new TanHungLong.Report.rpTongHopPhieuXuatDC(TuNgay, KhachHang, TrangThaiThanhToan, rPhieuXuatDCBOColecction);
            rTongHopPhieuXuatDC.ShowPreviewDialog();
        }

        private void btnInChiTiet_Click(object sender, EventArgs e)
        {

            int PhieuXuatDCID = StaticUtil.GetInt(gridViewPhieuXuatDC.GetFocusedRowCellValue("PhieuXuatDCID"), -1);
            if (PhieuXuatDCID >= 0)
            {
                this.PhieuXuatDCBO = Model.PhieuXuatDCBO.GetRootPhieuXuatDCBO(PhieuXuatDCID);
                this.PhieuXuatDCChiTietBOCollection = Model.PhieuXuatDCChiTietBOCollection.GetPhieuXuatDCChiTietBOCollection("PhieuXuatDCID = " + PhieuXuatDCID.ToString());

                RPhieuXuatDCBO rPhieuXuatDCBO = RPhieuXuatDCBO.NewRootRPhieuXuatDCBO();
                rPhieuXuatDCBO.CopyFromRPhieuXuatDCBO(PhieuXuatDCBO);
                Report.rpPhieuXuatDC rPhieuXuatDCHang = new TanHungLong.Report.rpPhieuXuatDC(rPhieuXuatDCBO, PhieuXuatDCChiTietBOCollection);
                rPhieuXuatDCHang.ShowPreviewDialog();
            }
            else
                MessageBox.Show("Chưa chọn Hóa đơn để in??", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }
}